<template>
  <el-dialog v-model="dialogFormVisible" draggable custom-class="dialog-title" :title="title" :close-on-click-modal="false" destroy-on-close>
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="width: 80%; margin-left:50px;">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="上级" prop="parentIds">
            <el-cascader
              v-model="temp.parentId"
              :options="list"
              :props="treeProps"
              :show-all-levels="false"
              label="name"
              change-on-select
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="机构名称" prop="name">
            <el-input v-model="temp.name" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="地理编码" prop="geocoding">
            <el-input v-model="temp.geocoding" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="邮政编码" prop="postalCode">
            <el-input v-model="temp.postalCode" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="dialogFormVisible = false">
        取消
      </el-button>
      <el-button v-permission="['test:treeTable:TreeTable:update']" type="primary" :loading="loading" @click="title==='新增'?createData():updateData()">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import { createTreeTable, updateTreeTable, getTreeTable } from '@/api/demo/treeTable/treeTable'
// import { ref } from 'vue'

export default {
  name: 'TreeTableForm',
  emits: ['refreshList'],
  data() {
    return {
      rules: {
        name: [{ required: true, message: '机构名称为必填项', trigger: 'blur' }],
        geocoding: [{ required: true, message: '地理编码为必填项', trigger: 'blur' }],
        postalCode: [{ required: true, message: '邮政编码为必填项', trigger: 'blur' }]
      },
      temp: {},
      list: [],
      treeProps: {
        value: 'id',
        label: 'name',
        checkStrictly: true,
        emitPath: false,
        expandTrigger: 'hover'
      },
      title: undefined,
      loading: false,
      dialogFormVisible: false
    }
  },
  methods: {
    /**
     * 设置列表数据
     * @param list 列表数据
     */
    setList(list) {
      this.list = list
    },
    /**
     * 获取列表数据
     * @param data
     */
    getList(data) {
      this.$emit('refreshList', data)
    },
    /**
     * 重置表单
     */
    resetTemp() {
      this.loading = false
      this.parentIds = undefined
      this.temp = {
        id: undefined,
        createBy: undefined,
        createDate: undefined,
        updateBy: undefined,
        updateDate: undefined,
        delFlag: undefined,
        tenantId: undefined,
        name: undefined,
        parentId: undefined,
        geocoding: undefined,
        postalCode: undefined
      }
    },
    /**
     * 新增
     */
    handleCreate() {
      this.resetTemp()
      this.title = '新增'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    /**
     * 提交新增
     */
    createData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          createTreeTable(this.temp).then(response => {
            this.loading = false
            if (response.data.success) {
              this.getList(response.data.data)
              this.dialogFormVisible = false
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    /**
     * 编辑
     */
    handleUpdate(id) {
      this.resetTemp()
      this.title = '编辑'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
      getTreeTable(id).then(response => {
        if (response.data.success) {
          this.temp = response.data.data
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    /**
     * 提交编辑
     */
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          updateTreeTable(this.temp).then(response => {
            this.loading = false
            if (response.data.success) {
              this.dialogFormVisible = false
              this.getList(this.temp)
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
